/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.actions
{
	import org.cocos2d.CCCamera;
	import org.cocos2d.basenodes.CCNode;
	import org.cocos2d.cocoa.CCObject;
	import org.cocos2d.cocoa.CCPoint;
	import org.cocos2d.cocoa.CCPoint3;
	import org.cocos2d.cocoa.CCZone;
	import org.cocos2d.support.ccMacros;
	
	public class CCOrbitCamera extends CCActionCamera
	{
		public function CCOrbitCamera()
		{
			m_fRadius = 0;
			m_fDeltaRadius = 0;
			m_fAngleZ = 0;
			m_fDeltaAngleZ = 0;
			m_fAngleX = 0;
			m_fDeltaAngleX = 0;
			m_fRadZ = 0;
			m_fRadDeltaZ = 0;
			m_fRadX = 0;
			m_fRadDeltaX = 0;
		}
		
		[Deprecated]
		public static function actionWithDuration(t : Number, radius : Number, deltaRadius : Number, angleZ : Number, deltaAngleZ : Number, angleX : Number, deltaAngleX : Number)
		{
			return CCOrbitCamera.create(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX);
		}
		
		public static function create(t : Number, radius : Number, deltaRadius : Number, angleZ : Number, deltaAngleZ : Number, angleX : Number, deltaAngleX : Number)
		{
			var pRet : CCOrbitCamera = new CCOrbitCamera();
			if (pRet.initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX))
			{
				return pRet;
			}
			
			return null;
		}
		
		public function initWithDuration(t : Number, radius : Number, deltaRadius : Number, angleZ : Number, deltaAngleZ : Number, angleX : Number, deltaAngleX : Number) : Boolean
		{
			if (super.initWithDuration(t))
			{
				m_fRadius = radius;
				m_fDeltaRadius = deltaRadius;
				m_fAngleZ = angleZ;
				m_fDeltaAngleZ = deltaAngleZ;
				m_fAngleX = angleX;
				m_fDeltaAngleX = deltaAngleX;
				
				m_fRadDeltaZ = ccMacros.CC_DEGREES_TO_RADIANS(deltaAngleZ);
				m_fRadDeltaX = ccMacros.CC_DEGREES_TO_RADIANS(deltaAngleX);
				return true;
			}
			
			return false;
		}
		
		public function sphericalRadius(param : CCPoint3) : void
		{
			var ex : Number, ey : Number, ez : Number, cx : Number, cy : Number, cz : Number, x : Number, y : Number, z : Number;
			var y : Number;
			var s : Number;
			
			var pCamera : CCCamera = m_pTarget.Camera;
			var retParam : CCPoint3 = new CCPoint3();
			pCamera.getEyeXYZ(retParam);
			ex = retParam.x; ey = retParam.y; ez = retParam.z;
			pCamera.getCenterXYZ(retParam);
			cx = retParam.x; cy = retParam.y; cz = retParam.z;
			
			x = ex - cx;
			y = ey - cy;
			z = ez - cz;
			
			var r : Number = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
			var s : Number = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
			if (s == 0.0)
				s = ccMacros.FLT_EPSILON;
			if (r == 0.0)
				r = ccMacros.FLT_EPSILON;
			
			param.y = Math.acos(z / r);
			if (x < 0)
				param.z = Math.PI - Math.asin(y / s);
			else
				param.z = Math.asin(y / s);
			
			param.x = r / CCCamera.getZEye();
		}
		
		public function copyWithZone(pZone : CCZone) : CCObject
		{
			var pNewZone : CCZone = null;
			var pRet : CCOrbitCamera = null;
			if (pZone && pZone.m_pCopyObject)
				pRet = pZone.m_pCopyObject as CCOrbitCamera;
			else
			{
				pRet = new CCOrbitCamera();
				pZone = pNewZone = new CCZone(pRet);
			}
			
			super.copyWithZone(pZone);
			
			pRet.initWithDuration(m_fDuration, m_fRadius, m_fDeltaRadius, m_fAngleZ, m_fDeltaAngleZ, m_fAngleX, m_fDeltaAngleX);
			
			return pRet;
		}
		
		public function startWithTarget(pTarget : CCNode) : void
		{
			super.startWithTarget(pTarget);
			var param : CCPoint3 = new CCPoint3();
			this.sphericalRadius(param);
			if (isNaN(m_fRadius))
				m_fRadius = param.x;
			if (isNaN(m_fAngleZ))
				m_fAngleZ = ccMacros.CC_RADIANS_TO_DEGREES(param.y);
			if (isNaN(m_fAngleX))
				m_fAngleX = ccMacros.CC_RADIANS_TO_DEGREES(param.x);
			
			m_fRadZ = ccMacros.CC_DEGREES_TO_RADIANS(m_fAngleZ);
			m_fRadX = ccMacros.CC_DEGREES_TO_RADIANS(m_fAngleX);
		}
		
		public function update(dt : Number) : void
		{
			var r : Number = (m_fRadius + m_fDeltaRadius * dt) * CCCamera.getZEye();
			var za : Number = m_fRadZ + m_fRadDeltaZ * dt;
			var xa : Number = m_fRadX + m_fRadDeltaX * dt;
			
			var i : Number = Math.sin(za) * Math.cos(xa) * r + m_fCenterXOrig;
			var j : Number = Math.sin(za) * Math.sin(xa) * r + m_fCenterYOrig;
			var k : Number = Math.cos(za) * r + m_fCenterZOrig;
			
			m_pTarget.Camera.setEyeXYZ(i, j, k);
		}
		
		protected var m_fRadius : Number;
		protected var m_fDeltaRadius : Number;
		protected var m_fAngleZ : Number;
		protected var m_fDeltaAngleZ : Number;
		protected var m_fAngleX : Number;
		protected var m_fDeltaAngleX : Number;
		
		protected var m_fRadZ : Number;
		protected var m_fRadDeltaZ : Number;
		protected var m_fRadX : Number;
		protected var m_fRadDeltaX : Number;
	}
}